<!--
 * @Description: file content
 * @Author: oooh
 * @Date: 2024-04-17 10:08:22
 * @LastEditors: oooh
 * @LastEditTime: 2024-04-22 09:35:29
 * @FilePath: \Civil_Servant_Performance_PC\src\views\organization\civil-servant\components\Import.vue
-->
<template>
  <div>
    <!-- 用户导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="isOpen" append-to-body @close="handleClose" width="400px">
      <el-upload
        class="text-center"
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <!-- <div class="el-upload__tip" slot="tip"><el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的用户数据</div> -->
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size: 12px; vertical-align: baseline" @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="handleClose">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog title="导入结果" :visible.sync="isResultOpen" append-to-body @close="handleResultClose" width="400px">
      <div>
        <p>本次共导入{{ importResult.successCnt + importResult.failCnt }}条数据</p>
        <p>其中成功 {{ importResult.successCnt }} 条，失败 {{ importResult.failCnt }} 条</p>
        <p>
          <el-link
            type="primary"
            :underline="false"
            style="font-size: 12px; vertical-align: baseline"
            @click="importErrorFile(importResult.excelUrl)"
          >
            下载错误数据
          </el-link>
        </p>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleResultClose">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getToken } from '@/utils/auth'
import { getConfigKey } from '@/api/system/config.js'
export default {
  name: 'civil-servant-import',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    queryParams: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      importResult: {},
      isResultOpen: false,
      isOpen: false,
      baseUrl: process.env.VUE_APP_BASE_API,
      // 用户导入参数
      upload: {
        // 弹出层标题（用户导入）
        title: '用户信息导入',
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        // updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: 'Bearer ' + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + '/api/civilServant/excel/imports'
      }
    }
  },
  watch: {
    visible: {
      handler(val) {
        this.isOpen = val
      }
    }
  },
  methods: {
    handleResultClose() {
      this.isResultOpen = false
    },
    importErrorFile(url) {
      this.$download.saveAs(this.baseUrl + url, `用户信息_异常数据_${new Date().getTime()}.xlsx`)
    },
    /** 下载模板操作 */
    importTemplate() {
      getConfigKey('biz.civilServant.excelTemplateUrl').then(res => {
        this.$download.saveAs(res.msg, `用户信息_导入模板_${new Date().getTime()}.xlsx`)
      })
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true
    },
    // 文件上传成功处理
    handleFileSuccess(res, file, fileList) {
      console.log('response', res)
      let { data } = res
      this.upload.isUploading = false
      this.$refs.upload.clearFiles()
      this.$emit('success')
      this.handleClose()
      this.importResult = data
      this.isResultOpen = true
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit()
    },
    // 关闭弹框
    handleClose() {
      this.$emit('update:visible', false)
    }
  }
}
</script>

<style></style>
